# -*- coding: utf-8 -*-
from source import *
from parse import *
from image import *
from hashlib import md5

class BingWWBig(Source):
    def __init__(self):
        super(BingWWBig, self).__init__()
        self.sourceInfo['name'] = 'bingWWBig'
        self.sourceInfo['domain'] = 'http://www.bing.com/'
        self.sourceInfo['startUrl'] = 'http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=en-ww/'
        self.sourceInfo['watermark'] = True
        self.sourceInfo['type'] = 'single'

    def setKey(self):
        parse = Prase(self.sourceInfo['startUrl'])
        str = parse.getValuesByPath('//images/image/fullstartdate')[0]
        if str == None:
            return None
        else:
            m = md5()
            m.update(str)
            self.imgInfo['key'] =  m.hexdigest()

    def setImgInfo(self):
        parse = Prase(self.sourceInfo['startUrl'])
        copyright = parse.getValuesByPath('//images/image/copyright')[0]
        copyrightsource = parse.getValuesByPath('//images/image/copyrightsource')[0]
        self.imgInfo['name'] = copyright.replace(' (' + copyrightsource + ')', '')
        copyrightsources = copyrightsource[2 :].split('/')
        if len(copyrightsources) == 1:
            self.imgInfo['company'] = copyrightsources[0]
        elif len(copyrightsources) == 2:
            self.imgInfo['author'] = copyrightsources[0]
            self.imgInfo['company'] = copyrightsources[1]
        self.imgInfo['publicDatetime'] = parse.getValuesByPath('//images/image/fullstartdate')[0]
        urlBase = 'http://www.bing.com' + parse.getValuesByPath('//images/image/urlBase')[0] + '_1920x1200.jpg'
        image = Image(urlBase)
        self.imgInfo['urls'] = {}
        self.imgInfo['urls']['base'] = [{'url':urlBase, 'width':image.getWidth(), 'height':image.getHeight()}]